package com.chushouya.product.dao.repository;

import com.general.framework.core.lang.Lists;
import com.general.framework.core.lang.Strings;
import com.general.framework.core.schema.PageResult;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.product.dao.entity.SkuEvaluateItemValueEntity;
import com.chushouya.product.dao.mapper.SkuEvaluateItemValueEntityMapper;
import com.chushouya.product.dto.admin.skuevaluate.SkuEvaluateItemValueQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Repository
public class SkuEvaluateItemValueRepository extends BaseRepository<SkuEvaluateItemValueEntityMapper, SkuEvaluateItemValueEntity, Long> {
    
    public List<SkuEvaluateItemValueEntity> selectList(SkuEvaluateItemValueQuery query) {
        Example example = buildExample(query);
        return this.selectByExample(example);
    }

    public PageResult<SkuEvaluateItemValueEntity> selectPageResult(SkuEvaluateItemValueQuery query) {
        Example example = this.buildExample(query);
        return this.selectPage(query.getPageRequest(), example);
    }
    
    public SkuEvaluateItemValueEntity selectOne(SkuEvaluateItemValueQuery query) {
        Example example = buildExample(query);
        return this.selectFirstOneByExample(example);
    }

    private Example buildExample(SkuEvaluateItemValueQuery query) {
        Example example = new Example(SkuEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(query.getSkuItemValueId())) {
            criteria.andEqualTo("skuItemValueId", query.getSkuItemValueId());
        }
        if(Lists.isNotEmpty(query.getSkuItemValueIdList())) {
            criteria.andIn("skuItemValueId", query.getSkuItemValueIdList());
        }
        if (Objects.nonNull(query.getSkuItemId())) {
            criteria.andEqualTo("skuItemId", query.getSkuItemId());
        }
        if (Objects.nonNull(query.getValueId())) {
            criteria.andEqualTo("valueId", query.getValueId());
        }
        if (Objects.nonNull(query.getItemId())) {
            criteria.andEqualTo("itemId", query.getItemId());
        }
        if (Objects.nonNull(query.getProductId())) {
            criteria.andEqualTo("productId", query.getProductId());
        }
        if (Strings.isNotEmpty(query.getValueName())) {
            criteria.andEqualTo("valueName", query.getValueName());
        }
        if (Objects.nonNull(query.getCalcType())) {
            criteria.andEqualTo("calcType", query.getCalcType());
        }
        if (Objects.nonNull(query.getStatus())) {
            criteria.andEqualTo("status", query.getStatus());
        }
        example.orderBy("sort").asc();
        return example;
    }
    
    public List<SkuEvaluateItemValueEntity> selectListBySkuItemId(Long skuItemId) {
        SkuEvaluateItemValueQuery query = new SkuEvaluateItemValueQuery();
        query.setSkuItemId(skuItemId);
        return selectList(query);
    }
    
    public List<SkuEvaluateItemValueEntity> selectListBySkuItemIds(List<Long> skuItemIds) {
        if (Lists.isEmpty(skuItemIds)) {
            return Lists.newArrayList();
        }
        Example example = new Example(SkuEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("skuItemId", skuItemIds);
        example.orderBy("sort").asc();
        return this.selectByExample(example);
    }
    
    public SkuEvaluateItemValueEntity selectBySkuItemIdAndValueId(Long skuItemId, Long valueId) {
        SkuEvaluateItemValueQuery query = new SkuEvaluateItemValueQuery();
        query.setSkuItemId(skuItemId);
        query.setValueId(valueId);
        return selectOne(query);
    }
    
    public List<SkuEvaluateItemValueEntity> selectByProductId(Long productId) {
        if (Objects.isNull(productId)) {
            return Lists.newArrayList();
        }
        Example example = new Example(SkuEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        example.orderBy("sort").asc();
        return this.selectByExample(example);
    }
    
    public int deleteBySkuItemIdAndProductId(Long skuItemId, Long productId) {
        if (Objects.isNull(skuItemId) || Objects.isNull(productId)) {
            return 0;
        }
        Example example = new Example(SkuEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("skuItemId", skuItemId);
        criteria.andEqualTo("productId", productId);
        return this.deleteByExample(example);
    }
    
    public int deleteByProductId(Long productId) {
        if (Objects.isNull(productId)) {
            return 0;
        }
        Example example = new Example(SkuEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        return this.deleteByExample(example);
    }
    
    public int deleteByProductId(Long productId, Long evaluateValueId) {
        if (Objects.isNull(productId) || Objects.isNull(evaluateValueId)) {
            return 0;
        }
        Example example = new Example(SkuEvaluateItemValueEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        criteria.andEqualTo("valueId", evaluateValueId);
        return this.deleteByExample(example);
    }
}
